package com.alibaba.citrus.cr.order.open.api.facade.convert;

import com.alibaba.citrus.cr.order.open.api.facade.model.TransferOrder;
import com.alibaba.citrus.cr.order.open.api.facade.model.TransferSubOrder;
import com.alibaba.cz.base.model.store.CargoZoneSDO;
import com.alibaba.cz.base.tool.DateFormatUtil;
import com.alibaba.cz.base.tool.FeatureUtil;
import com.alibaba.cz.base.tool.Nullable;
import com.epoch.app.crunifiedinventory.domain.allocateorder.model.AllocateOrder;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.model.AllocateOrderDetail;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: yuhw
 * @date: 2024年01月09日 17:18
 **/
@Component
public class TransferOrder2AllocateOrderConvert {

    @Resource
    private WarehouseConvert warehouseConvert;

    public TransferOrder backward(AllocateOrder allocateOrder, List<AllocateOrderDetail> detailList){
        if (Objects.isNull(allocateOrder)){
            return null;
        }
        TransferOrder transferOrder = new TransferOrder();
        transferOrder.setTransferOrderCode(allocateOrder.getCode());
        transferOrder.setOutAllocateOrderId(allocateOrder.getOutAllocateOrderId());
        transferOrder.setBusinessType(allocateOrder.getBusinessType());
        transferOrder.setFromSys(allocateOrder.getFromSys());
        transferOrder.setPriority(allocateOrder.getPriority() == null ? null : allocateOrder.getPriority().toString());
        Map<String, CargoZoneSDO> cargoZoneSDOMap = warehouseConvert.getCargoZoneSDOMap(Arrays.asList(allocateOrder.getShipAreaId(), allocateOrder.getReceiveAreaId())
                .stream().filter(StringUtils::isNotBlank).collect(Collectors.toList()));
        transferOrder.setLogicShipWarehouseCode(Optional.ofNullable(cargoZoneSDOMap.get(allocateOrder.getShipAreaId()))
                .map(CargoZoneSDO::getCode).orElse(null));
        transferOrder.setLogicReceiveWarehouseCode(Optional.ofNullable(cargoZoneSDOMap.get(allocateOrder.getReceiveAreaId()))
                .map(CargoZoneSDO::getCode).orElse(null));
        transferOrder.setProductGroupCode(allocateOrder.getProductGroup());
        //TODO
        transferOrder.setSalesOrganizationCode(allocateOrder.getSapSalesOrgId());
        transferOrder.setCreator(allocateOrder.getCreator());
        if (allocateOrder.getExpectedDeliveryDate() != null){
            transferOrder.setExpectedDeliveryTime(DateFormatUtil.formatToyyyMMdd(allocateOrder.getExpectedDeliveryDate()));
        }
        transferOrder.setShippingMethodCode(allocateOrder.getShipMethod() == null ? null : allocateOrder.getShipMethod().toString());
        transferOrder.setRemark(allocateOrder.getRemark());
        transferOrder.setTransportationMethod(allocateOrder.getShipMethod() == null ? null : allocateOrder.getShipMethod().toString());
        transferOrder.setTransferOrderType(allocateOrder.getType() == null ? null : allocateOrder.getType().toString());
        transferOrder.setRelatedAllocateOrderCode(allocateOrder.getRelatedAllocateOrderCode());
        transferOrder.setAllocateApplyOrderCode(allocateOrder.getAllocateApplyOrderCode());
        transferOrder.setAllocateApplyOrderId(allocateOrder.getAllocateApplyOrderId());
        transferOrder.setBizFeatures(FeatureUtil.fromString(allocateOrder.getBizFeatures()));
        transferOrder.setShipWarehouseId(allocateOrder.getShipWarehouseId());
        transferOrder.setReceiveWarehouseId(allocateOrder.getReceiveWarehouseId());
        transferOrder.setShipAreaId(allocateOrder.getShipAreaId());
        transferOrder.setReceiveAreaId(allocateOrder.getReceiveAreaId());
        transferOrder.setFulfillmentTypeCode(allocateOrder.getFulfillmentTypeCode());
        //运输方式
        transferOrder.setShipMethod(allocateOrder.getShipMethod());
        //货场仓的code
        transferOrder.setTransferWarehouseId(allocateOrder.getTransferWarehouseId());
        //是否退单
        transferOrder.setReturnOrder(allocateOrder.getReturnOrder());
        transferOrder.setApplyOrgId(allocateOrder.getApplyOrgId());

        List<TransferSubOrder> transferSubOrders = Lists.newArrayList();
        Nullable.stream(detailList).forEach(detail -> {
            TransferSubOrder transferSubOrder = new TransferSubOrder();
            transferSubOrder.setTransferOrderCode(allocateOrder.getCode());
            transferSubOrder.setLineNo(detail.getLineNo() == null ? null : detail.getLineNo().toString());
            transferSubOrder.setOutAllocateOrderLineId(detail.getOutAllocateOrderLineId());
            transferSubOrder.setMaterialCode(detail.getScItemCode());
            //TODO 生产日期待定
            if (detail.getExpectedDeliveryDate() != null){
                transferSubOrder.setProductionDate(DateFormatUtil.formatToyyyMMdd(detail.getExpectedDeliveryDate()));
            }
            transferSubOrder.setQuantity(detail.getQuantity());
            transferSubOrder.setLineRemark(detail.getRemarks());
            if (detail.getGmt_create() != null){
//                transferSubOrder.setOrderCreateTime(DateFormatUtil.format(detail.getGmt_create()));
            }
            transferSubOrder.setFeature(detail.getFeatures());
            transferSubOrder.setClose(detail.getClose());
            transferSubOrder.setCloseReasonCode(detail.getCloseCode());
            transferSubOrder.setCloseReason(detail.getCloseReason());

            transferSubOrder.setRelatedAllocateOrderSubId(detail.getRelatedAllocateSubOrderId());
            transferSubOrder.setAllocateApplyOrderLineId(detail.getAllocateApplyOrderLineId());
            transferSubOrder.setBizFeatures(FeatureUtil.fromString(detail.getBizFeatures()));
            transferSubOrder.setUnit(detail.getUnit());
            transferSubOrder.setGmtCreate(detail.getGmt_create());
            transferSubOrder.setGmtModified(detail.getGmt_modified());
            transferSubOrders.add(transferSubOrder);

        });
        transferOrder.setDetailList(transferSubOrders);
        return transferOrder;

    }
}
